package com.rhb.erp.hr.person.web.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.*;

import com.rhb.erp.hr.person.bean.*;
import com.rhb.erp.hr.person.web.struts.form.*;
import com.rhb.erp.web.struts.action.ErpBaseDispatchAction;
import com.rhb.exception.DuplicateException;
import com.rhb.exception.RequiredException;

public class PersonSaveAction extends ErpBaseDispatchAction {
	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		//System.out.println("unspecified for defalut parameter!");
		return mapping.findForward("fail");
	}
	public ActionForward del(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		PersonForm oform = (PersonForm)form;
		this.getPersonBusiness().delete(oform.getPerson());
		return mapping.findForward("success");
	}
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		PersonForm oform = (PersonForm)form;
		ActionMessages messages = new ActionMessages();
		try {
			this.getPersonBusiness().update(oform.getPerson());
		} catch (DuplicateException e) {
			messages.add("errors.duplicate", new ActionMessage("errors.duplicate",e.toString()));
			saveErrors(request, messages);
			return mapping.findForward("editFail");
		}catch (RequiredException e) {
			messages.add("errors.required", new ActionMessage("errors.required",e.toString()));
			saveErrors(request, messages);
			return mapping.findForward("editFail");
		}
		return mapping.findForward("success");
	}
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		PersonForm oform = (PersonForm)form;
		ActionMessages messages = new ActionMessages();
		try {
			this.getPersonBusiness().create(oform.getPerson());
		} catch (DuplicateException e) {
			messages.add("errors.duplicate", new ActionMessage("errors.duplicate",e.toString()));
			saveErrors(request, messages);
			return mapping.findForward("addFail");
		}catch (RequiredException e) {
			messages.add("errors.required", new ActionMessage("errors.required",e.toString()));
			saveErrors(request, messages);
			return mapping.findForward("addFail");
		}
		return mapping.findForward("success");
	}	 
}
